#!/usr/bin/env python
# encoding: utf-8
"""
fileutils.py

Created by Orestis Markou on 2007-11-01.
Copyright (c) 2007 Orestis Markou. All rights reserved.
"""

import sys
import os

def parse_word_file(filename, pattern, bins, encoding='utf-8'):
    """docstring for parse_word_file"""
    result = []
    # for key in bins:
    #     result[key] = []
    file = open(filename, 'rb')
    contents = file.read() 
    if contents.startswith('\xef\xbb\xbf'):
        contents = contents[3:] #strip BOM       
    contents = unicode(contents, encoding)
    for line in contents.splitlines():
        m = pattern.match(line)
        if m is None: continue
        record = {}
        for key in m.groupdict():
            if key in bins:
                value = m.groupdict()[key]
                if value.isdigit():
                    record[key]=float(value)
                else:
                    record[key]=value
                # print record
        if record:        
            result.append(record)
                     
    return result

if __name__ == '__main__':
    import re
    pattern = re.compile(r'(?P<greek>\w+)-(?P<count>\d+).*', re.UNICODE)
    print pattern.match('και-664493')
    print pattern.match('να-645987-(0.030625033482)')
    bins = ['greek', 'count']
    filename = 'greek.txt'
    res =    parse_word_file(filename, pattern, bins)
    print res
